package top.crossrun.webview.bridge.webview.x5;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import com.tencent.smtt.export.external.TbsCoreSettings;
import com.tencent.smtt.sdk.QbSdk;
import com.tencent.smtt.sdk.ValueCallback;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;

import java.util.HashMap;

/**
 * create by Run
 * on 2020/8/24 13:35
 * do
 */
public class X5WebViewHelper {

    /**
     * Application里面的初始化
     *
     * @param application
     */
    public static void initOnApplication(Application application) {
        HashMap map = new HashMap();
        map.put(TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER, true);
        map.put(TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE, true);
        QbSdk.initTbsSettings(map);

        QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {

            @Override
            public void onViewInitFinished(boolean arg0) {
                // TODO Auto-generated method stub
                //x5內核初始化完成的回调，为true表示x5内核加载成功，否则表示x5内核加载失败，会自动切换到系统内核。
//                Log.d("app", " onViewInitFinished is " + arg0);
            }

            @Override
            public void onCoreInitFinished() {
                // TODO Auto-generated method stub
            }
        };
        //x5内核初始化接口
        QbSdk.initX5Environment(application, cb);
    }

    /**
     * @param activity    activity
     * @param webView     webview
     * @param code        发起文件选择时候的code
     * @param frameLayout 全屏播放的容器
     * @param progressBar 进度条，可以为空
     */
    public static void initWebView(Activity activity, WebView webView, int code, FrameLayout frameLayout, ProgressBar progressBar) {
        initWebView(webView, new X5WebViewChromeClient(activity, code, frameLayout, progressBar));
    }

    /**
     * 对webview初始化
     *
     * @param webView
     */
    public static void initWebView(WebView webView, WebChromeClient webChromeClient) {
        WebSettings webSetting = webView.getSettings();
        webSetting.setJavaScriptEnabled(true);
        webSetting.setJavaScriptCanOpenWindowsAutomatically(true);
        webSetting.setAllowFileAccessFromFileURLs(true);
        webSetting.setAllowUniversalAccessFromFileURLs(true);
        webSetting.setAllowFileAccess(true);
        webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSetting.setSupportZoom(true);
        webSetting.setBuiltInZoomControls(true);
        webSetting.setUseWideViewPort(true);
        webSetting.setSupportMultipleWindows(true);
        // webSetting.setLoadWithOverviewMode(true);
        webSetting.setAppCacheEnabled(true);
        // webSetting.setDatabaseEnabled(true);
        webSetting.setDomStorageEnabled(true);
        webSetting.setGeolocationEnabled(true);
        webSetting.setAppCacheMaxSize(Long.MAX_VALUE);
        // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
        webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
        // webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH);
        webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE);
        if (webChromeClient != null) {
            webView.setWebChromeClient(webChromeClient);
        }
    }


    /**
     * @param context
     * @param filePath 文件地址
     * @return 1:用 QQ 浏览器打开<br>
     * 2:用 MiniQB 打开<br>
     * 3:调起阅读器弹框<br>
     * -1:filePath 为空 打开失败<br>
     */
    public static int openFileReader(Context context, String filePath) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("style", "1");//“0”表示文件查看器使用默认的 UI 样式。“1”表示文件查看器使用微信的 UI 样式。不设置此 key或设置错误值，则为默认 UI 样式。
        params.put("local", "true");//“true”表示是进入文件查看器，如果不设置或设置为“false”，则进入 miniqb 浏览器模式。
//        params.put("topBarBgColor:","#06A7FF");//定制文件查看器的顶部栏背景色。格式为“#xxxxxx”
//        params.put("memuData", jsondata);
        return openFileReader(context, filePath, params, new ValueCallback<String>() {
            @Override
            public void onReceiveValue(String s) {

            }
        });
    }

    /**
     * @param context
     * @param filePath 文件地址
     * @param params   配置信息
     * @param callback
     * @return 1:用 QQ 浏览器打开<br>
     * 2:用 MiniQB 打开<br>
     * 3:调起阅读器弹框<br>
     * -1:filePath 为空 打开失败<br>
     */
    public static int openFileReader(Context context, String filePath, HashMap<String, String> params, ValueCallback<String> callback) {
        return QbSdk.openFileReader(context, filePath, params, callback);
    }
}
